package com.example.springbootbabyu.service.impl;

import com.example.springbootbabyu.entity.*;
import com.example.springbootbabyu.mapper.HomeMapper;
import com.example.springbootbabyu.service.HomeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 功能
 * 作者：梦天
 * 日期：2025/4/15 13:57
 */
@Service
@Slf4j
public class HomeServiceImpl implements HomeService {
    @Autowired
    HomeMapper homeMapper;

    public List<Category> getHomeCategoryHead() {
        // 1. 查询所有一级分类
        List<Category> rootCategories = homeMapper.selectRootCategories();

        // 2. 为每个分类加载商品
        rootCategories.forEach(category ->
                category.setChildren(homeMapper.selectGoodsByCategory(category.getId()))
        );

        return rootCategories;
    }
    public List<Brand> getHomeBrand(){
        List<Brand> brands = homeMapper.selectBrands();
        return brands;
    }

    @Override
    public List<Hot> getHomeHot() {
        List<Hot> list = homeMapper.getHomeHot();
        return list;
    }

    @Override
    public List<New> getHomeNew() {
        List<New> list = homeMapper.getHomeNew();
        return list;
    }
    @Override
    public List<Banner> getHomeBanner() {
        List<Banner> list = homeMapper.getHomeBanner();
        return list;
    }

    @Override
    public List<HomeGoods> getHomeGoods(){
        // 1. 查询所有一级分类
        List<HomeGoods> topCategories = homeMapper.selectTopCategories();

        // 2. 为每个一级分类填充子分类和商品
        topCategories.forEach(category -> {
            // 查询子分类
            List<HomeGoodsChildren> children = homeMapper.selectChildrenByParentId(category.getId());
            category.setChildren(children);

            // 查询推荐商品
            List<HomeGoodsDTO> goods = homeMapper.selectGoodsByCategoryId(category.getId());
            category.setGoods(goods);
        });

        return topCategories;
    }
}
